//
//  NotesCommentTableViewCell.m
//  TravelFirst
//
//  Created by lanou on 15/9/24.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "NotesCommentTableViewCell.h"

@implementation NotesCommentTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.backgroundV = [[UIView alloc] init];
        self.backgroundV.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.backgroundV];
        
        self.avatarButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.avatarButton.frame = CGRectMake(10, 10, 40, 40);
        self.avatarButton.layer.masksToBounds = YES;
        self.avatarButton.layer.cornerRadius = self.avatarButton.width / 2;
        [self.backgroundV addSubview:self.avatarButton];
        
        self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarButton.right + 10, self.avatarButton.top + 5, kWidth - 20 - self.avatarButton.right - 10 - 15, 30)];
        self.contentLabel.numberOfLines = 0;
        //self.contentLabel.backgroundColor = [UIColor purpleColor];
        [self.backgroundV addSubview:self.contentLabel];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(self.contentLabel.left, self.contentLabel.bottom + 10, self.contentLabel.width, 1)];
        lineView.backgroundColor = kColor;
        lineView.tag = 100;
        [self.backgroundV addSubview:lineView];
        
        self.backgroundV.frame = CGRectMake(10, 0, kWidth - 20, lineView.bottom);
        
    }
    return self;
}


#pragma mark - 赋值 -

- (void)setValueWithModel:(NotesModel *)model
{
    [self.avatarButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.avatar] forState:UIControlStateNormal placeholderImage:nil];
    [self setStringAttributeWithModel:model];
}

// 评论框赋值
- (void)setStringAttributeWithModel:(NotesModel *)model
{
    NSMutableAttributedString *nameAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@: ", model.nickname]];
    [nameAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, model.nickname.length + 1)];
    
    NSString *createdDate = [NotesCommentTableViewCell transformDateWithString:model.created_date];
    NSMutableAttributedString *contentAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@", model.content, createdDate]];
    [contentAttStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(model.content.length + 1, createdDate.length)];
    
    [nameAttStr appendAttributedString:contentAttStr];
    self.contentLabel.attributedText = nameAttStr;
    
    CGFloat height = [NotesCommentTableViewCell getLabelHeightWithString:self.contentLabel.text];
    
    self.contentLabel.frame = CGRectMake(self.avatarButton.right + 10, self.avatarButton.top + 5, kWidth - 20 - self.avatarButton.right - 10 - 15, height);
    
    UIView *lineView = [self.backgroundV viewWithTag:100];
    lineView.frame = CGRectMake(self.contentLabel.left, self.contentLabel.bottom + 10, self.contentLabel.width, 1);
}

// cell高度
+ (CGFloat)appendStringWithModel:(NotesModel *)model
{
    NSString *string = [model.nickname stringByAppendingFormat:@": %@\n%@", model.content, [NotesCommentTableViewCell transformDateWithString:model.created_date]];
    return [NotesCommentTableViewCell getLabelHeightWithString:string];
}

+ (CGFloat)getLabelHeightWithString:(NSString *)string
{
    return [string boundingRectWithSize:CGSizeMake(280, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName] context:nil].size.height;
}

+ (NSString *)transformDateWithString:(NSString *)string
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[string floatValue]];
    NSString *dateStr = [formatter stringFromDate:date];
    
    return [dateStr substringToIndex:16];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
